home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / csim / source.lha / source / C++SIM / TestRandom.cc < prev    next >
C/C++ Source or Header  |  1993-06-14  |  3KB  |  93 lines

  1. /*
  2.  * Copyright (C) 1993
  3.  *
  4.  * Department of Computing Science,
  5.  * The University,
  6.  * Newcastle upon Tyne,
  7.  * UK.
  8.  */
  9.  
  10. #include <stdlib.h>    // to get getopt
  11. #include <iostream.h>
  12. #include "Random.h"
  13.  
  14. void Gen(int count, RandomStream *r, const char *title)
  15. {
  16.     // Create and initialise buckets for a bar graph
  17.     int i;
  18.     int bucket[100];
  19.     for (i=0; i<100; i++) bucket[i]=0;
  20.  
  21.     // generate numbers and update bucket counts
  22.     for (i=0; i<count; i++) {
  23.     double result = (*r)();
  24.     int idx = result < 0.0 ? 0 : (result > 100.0) ? 100 : (int) result;
  25.     bucket[idx]++;
  26.     }
  27.  
  28.     // output a title followed by the bucket counts
  29.     cout << endl << '"' << title << '"' << endl;
  30.     for (i=0; i<100; i++)
  31.     cout << i << ".0 " << bucket[i] << endl;
  32.  
  33. cerr << "Chi-Square error measure: " << r->Error() << endl;
  34. }
  35.  
  36. int main (int argc, char **argv)
  37. {
  38.     extern char *optarg;
  39.     extern int optind;
  40.     int c;
  41.     int count=10000;
  42.     int errflg = 0;
  43.     int uniform=0, normal=0, exponential=0, hyperexponential=0, erlang=0;
  44.     int skip=0;
  45.  
  46.     while ((c = getopt(argc, argv, "s:c:unxhe")) != -1)
  47.         switch(c) {
  48.         case 'c': count = atoi(optarg); break;
  49.         case 'u': uniform++; break;
  50.         case 'n': normal++; break;
  51.         case 'x': exponential++; break;
  52.         case 'h': hyperexponential++; break;
  53.         case 'e': erlang++; break;
  54.         case 's': skip = atoi(optarg); break;
  55.  
  56.         case '?':
  57.         default: errflg++;
  58.         }
  59.  
  60.     // ASSERT: argc-optind == number of arguments remaining
  61.     //         argv[optind] == first non-flag argument
  62.  
  63.     if (errflg || (argc-optind)) {
  64.         cerr << "usage: " << argv[0] << "[-c <number>][-u][-n][-x][-h][-e]" << endl;
  65.  
  66.         cerr << "\t-c 99\t\tgenerate 99 points for each distribution" << endl;
  67.         cerr << "\t-u\t\toutput a data set for a uniform distribution" << endl;
  68.         cerr << "\t-n\t\toutput a data set for a normal distribution" << endl;
  69.         cerr << "\t-x\t\toutput a data set for an exponential distribution" << endl;
  70.         cerr << "\t-h\t\toutput a data set for a hyperexponential distribution" << endl;
  71.         cerr << "\t-e\t\toutput a data set for an erlang distribution" << endl;
  72.         return 2;
  73.     }
  74.  
  75.     // output a normal distribution by default
  76.     if (!uniform && !normal && !exponential && !hyperexponential && !erlang)
  77.     normal++;
  78.  
  79.     cout << "TitleText: Random Number Distributions" 
  80.      << " (" << count << " numbers/distribution)" 
  81.      << endl;
  82.  
  83.     for (int q=0; q<skip; q++) delete new UniformStream(0.0, 100.0);
  84.  
  85.     if (uniform) Gen (count, new UniformStream (0.0, 100.0), "Uniform");
  86.     if (normal) Gen (count, new NormalStream (50.0, 15.0), "Normal");
  87.     if (exponential) Gen (count, new ExponentialStream(50.0), "Exponential");
  88.     if (hyperexponential) Gen (count, new HyperExponentialStream(50.0, 55.0), "HyperExponential");
  89.     if (erlang) Gen (count, new ErlangStream(50.0, 15.0), "Erlang");
  90.  
  91.     return 0;
  92. }
  93.